package com.xywl.escort.view.pwd

import android.os.Bundle
import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle2.LifecycleTransformer
import com.xywl.escort.R
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.BaseResp
import com.xywl.escort.bean.UpdatePayPwdReq
import com.xywl.escort.common.Constant
import com.xywl.escort.common.PreferenceKeys.USER_ID
import com.xywl.escort.utils.MD5
import com.xywl.escort.utils.RxSPTool
import com.xywl.escort.utils.ToastUtil
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_modify_pwd.*
import javax.inject.Inject

@Route(path = "/xy/pwd/modify/pay")
class ModifyPayPwdActivity : BaseActivity(), ModifyPayPwdContract.View {
    @Autowired
    @JvmField
    var type = 1

    @Inject
    lateinit var mPresenter: ModifyPayPwdContract.Presenter

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_modify_pay_pwd)
        ARouter.getInstance().inject(this)

        mPresenter.takeView(this)
        initView()
        addListener()
    }

    private fun initView() {
        showBackButton()
        setTitleText("更换支付密码")
    }

    private fun addListener() {
        text_recover_pwd.setOnClickListener {
            ARouter.getInstance().build("/xy/pwd/recover/pay").navigation()
        }

        btn_confirm.setOnClickListener {
            val oldPwd = edit_old_pwd.text.toString().trim()
            val newPwd = edit_new_pwd.text.toString().trim()
            val confirmPwd = edit_confirm_pwd.text.toString().trim()

            if (checkPayPwd(oldPwd, newPwd, confirmPwd)) {
                modifyPayPwd(oldPwd, newPwd)
            }
        }
    }

    private fun checkPayPwd(oldPwd: String, newPwd: String, confirmPwd: String): Boolean {
        if (TextUtils.isEmpty(oldPwd)) {
            ToastUtil.showTip(this, R.string.user_account_hint_old_password)
            return false
        } else if (TextUtils.isEmpty(newPwd) || TextUtils.isEmpty(confirmPwd)) {
            ToastUtil.showTip(this, R.string.user_account_hint_new_password)
            return false
        } else if (newPwd != confirmPwd) {
            ToastUtil.showTip(this, R.string.user_account_hint_password_error)
            return false
        } else if (newPwd.length < Constant.Length.MIN_PAY_PWD_LENGTH) {
            ToastUtil.showTip(this, R.string.user_account_reset_pay_password_error)
            return false
        }
        return true
    }

    private fun modifyPayPwd(oldPwd: String, newPwd: String) {
        val req = UpdatePayPwdReq()
        req.userId = RxSPTool.getString(this, USER_ID)
        req.oldPayPwd = MD5.encrypt(oldPwd)
        req.newPayPwd = MD5.encrypt(newPwd)
        mPresenter.modifyPayPwd(req)
    }

    override fun isActive(): Boolean {
        return false
    }

    override fun <T : Any?> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    override fun modifyPaySuccess(resp: BaseResp?) {
        ToastUtil.showTip(this, "修改支付密码成功")
        finish()
    }

    override fun modifyPayFailure(msg: String?) {
        ToastUtil.showTip(this, msg)
    }
}
